package org.liang.job.common;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;


/**
 * ID 实体.
 * 
 * @author L.Yang
 * @version 1.0, 2015年9月24日
 */
public class IDBuilder {

    private IDBuilder() {}

    /** 默认名称. */
    private static final String DEFAULT_NAME = "清软创新";

    /** 字符集 . */
    private static final String CHARSET = "UTF_8";

    /**
     * 获取唯一id .
     * 
     * @param name
     *            分组名称
     * @return 50位唯一id
     */
    public static String buildUniqueID(String name) {
        name = StringUtils.isEmpty(name) ? DEFAULT_NAME : name;
        String str1 = UUID.randomUUID().toString();
        String val = str1;
        try {
            String str2 = UUID.nameUUIDFromBytes(name.getBytes(CHARSET)).toString();
            val = String.format("%s-%s", str2.substring(24), str1);
        } catch (UnsupportedEncodingException e) {
            // could not happened exception
        }
        return val;
    }

    /**
     * 获取唯一id.
     * 
     * @return 50位唯一id
     */
    public static String buildUniqueID() {
        String str1 = UUID.randomUUID().toString();
        String val = str1;
        try {
            String str2 = UUID.nameUUIDFromBytes(DEFAULT_NAME.getBytes(CHARSET)).toString();
            val = String.format("%s-%s", str2.substring(24), str1);
        } catch (UnsupportedEncodingException e) {
            // could not happened exception
        }
        return val;
    }

}
